package com.springboot.service.impl;

import com.springboot.dto.ChatContentView;
import com.springboot.dto.FriendsList;
import com.springboot.mapper.ChatRecordMapper;
import com.springboot.mapper.UserLoginFormMapper;
import com.springboot.model.ChatRecord;
import com.springboot.model.UserLoginForm;
import com.springboot.service.OnlineConsultationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

@Service
public class OnlineConsultationServiceImpl implements OnlineConsultationService {
    @Autowired
    private ChatRecordMapper chatRecordMapper;
    @Autowired
    private UserLoginFormMapper userLoginFormMapper;

    @Override
    public ArrayList<FriendsList> showFriendList(Integer doctorId, Integer assistantId){
        ArrayList<ChatRecord> chatRecords = new ArrayList<>();
        try {
            chatRecords = chatRecordMapper.selectByDoctorId(doctorId);
            System.out.println(chatRecords.size());
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("PatientChatServiceImpl 出错");
        }
        if(chatRecords.size()==0){
            return null;
        }
        ArrayList<Integer> patientIds=new ArrayList<>();
        int num=0;//不重复patientId的个数
        for(int i=0;i<chatRecords.size();i++){
            ChatRecord chatRecord = chatRecords.get(i);
            int flag=1;
            if(!chatRecord.getSenderId().equals(doctorId)){
                for(int j=0;j<num;j++)
                {
                    if(chatRecord.getSenderId().equals(patientIds.get(j)))
                    {
                        flag=0;
                    }
                }
                if (flag==1){
                    patientIds.add(chatRecord.getSenderId());
                    num++;
                }
            }
            flag=1;
            if(!chatRecord.getReceiverId().equals(doctorId)){
                for(int j=0;j<num;j++)
                {
                    if(chatRecord.getReceiverId().equals(patientIds.get(j)))
                    {
                        flag=0;
                    }
                }
                if (flag==1){
                    patientIds.add(chatRecord.getReceiverId());
                    num++;
                }
            }
        }
        System.out.println("助理聊天列表数量:"+num);
        //封装聊天列表，显示该助理最新的一条聊天记录
        ArrayList<FriendsList> friendsLists=new ArrayList<>();
        for(int i=0;i<num;i++)
        {
            int unReadNum=0;
            System.out.println("与医生"+doctorId+"聊天的患者:"+patientIds.get(i));
            FriendsList friendsList=new FriendsList();
            try {
                chatRecords = chatRecordMapper.selectCustomerServiceAllChat(doctorId,patientIds.get(i));
            }
            catch (Exception e)
            {
                e.printStackTrace();
                System.out.println("显示聊天列表错误");
            }
            if(patientIds.get(i)!=assistantId){
                for(int j=0;j<chatRecords.size();j++){
                    if(chatRecords.get(j).getReceiverState().equals("未读")&&chatRecords.get(j).getReceiverId().equals(doctorId)){
                        unReadNum++;
                    }
                }
            }
            else{
                for(int j=0;j<chatRecords.size();j++){
                    if(chatRecords.get(j).getReceiverState().equals("未读")&&chatRecords.get(j).getSenderId().equals(doctorId)){
                        unReadNum++;
                    }
                }
            }
            ChatRecord chatRecord = chatRecords.get(0);//获取最新的一条消息
            String content=chatRecord.getChatContent();
            Integer isPicture=chatRecord.getFlag2();
            UserLoginForm userLoginForm=new UserLoginForm();
            userLoginForm=userLoginFormMapper.selectByPrimaryKey(doctorId);
            String doctorName=userLoginForm.getUserName();
            String doctorTel=userLoginForm.getUserTel();
            String logoLink1=userLoginForm.getLogoLink();
            userLoginForm=userLoginFormMapper.selectByPrimaryKey(patientIds.get(i));
            String patientName=userLoginForm.getUserName();
            String patientTel=userLoginForm.getUserTel();
            String logoLink=userLoginForm.getLogoLink();
            //助理和医生的聊天列表
            if(userLoginForm.getUserId()==assistantId){
                patientName=doctorName;
                doctorName=null;
                patientTel=doctorTel;
                doctorTel=null;
                logoLink=logoLink1;
            }
            SimpleDateFormat f1=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String messageTime = f1.format(chatRecord.getChatTime());
            friendsList.setContent(content);
            friendsList.setDoctorName(doctorName);
            friendsList.setLogoLink(logoLink);
            friendsList.setName(patientName);
            friendsList.setTime(messageTime);
            if(unReadNum>0){
                friendsList.setIsRead(1);
            }
            System.out.println("unReadNum:"+unReadNum);
            friendsList.setIsPicture(isPicture);
            friendsList.setDoctorTel(doctorTel);
            friendsList.setPatientTel(patientTel);
            friendsLists.add(friendsList);
        }
        return friendsLists;
    }

    @Override
    public FriendsList getCustomerServiceInfo(){
        FriendsList friendsList1=new FriendsList();
        try{
            ArrayList<ChatRecord> chatRecords1=new ArrayList<>();
            chatRecords1=chatRecordMapper.selectCustomerServiceUnRead();
            if(chatRecords1.size()>0){
                friendsList1.setIsRead(1);
            }
            friendsList1.setName("客服");
        }
        catch (Exception e){
            System.out.println("chatRecordMapper错误");
            e.printStackTrace();
        }
        return  friendsList1;
    }

    @Override
    public ArrayList<FriendsList> showCustomerServiceList(){
        ArrayList<ChatRecord> chatRecords = new ArrayList<>();
        try {
            chatRecords = chatRecordMapper.selectCustomerServiceAllMessage();
            System.out.println(chatRecords.size());
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("PatientChatServiceImpl 出错");
        }
        if(chatRecords.size()==0){
            return null;
        }
        ArrayList<Integer> patientIds=new ArrayList<>();
        int num=0;//不重复patientId的个数
        for(int i=0;i<chatRecords.size();i++){
            ChatRecord chatRecord = chatRecords.get(i);
            int flag=1;
            if(!chatRecord.getSenderId().equals(0)){
                for(int j=0;j<num;j++)
                {
                    if(chatRecord.getSenderId().equals(patientIds.get(j)))
                    {
                        flag=0;
                    }
                }
                if (flag==1){
                    patientIds.add(chatRecord.getSenderId());
                    num++;
                }
            }
            flag=1;
            if(!chatRecord.getReceiverId().equals(0)){
                for(int j=0;j<num;j++)
                {
                    if(chatRecord.getReceiverId().equals(patientIds.get(j)))
                    {
                        flag=0;
                    }
                }
                if (flag==1){
                    patientIds.add(chatRecord.getReceiverId());
                    num++;
                }
            }
        }
        System.out.println("助理聊天列表数量:"+num);
        //封装聊天列表，显示该助理最新的一条聊天记录
        ArrayList<FriendsList> friendsLists=new ArrayList<>();
        for(int i=0;i<num;i++) {
            int unReadNum = 0;
            FriendsList friendsList = new FriendsList();
            try {
                chatRecords = chatRecordMapper.selectCustomerServiceAllChat(0,patientIds.get(i));
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("显示聊天列表错误");
            }
            System.out.println("聊天记录的总数 ："+chatRecords.size());
            ChatRecord chatRecord = chatRecords.get(0);//获取最新的一条消息
            String content = chatRecord.getChatContent();
            //System.out.println(content);
            Integer isPicture = chatRecord.getFlag2();
            UserLoginForm userLoginForm = userLoginFormMapper.selectByPrimaryKey(patientIds.get(i));
            String patientName = userLoginForm.getUserName();
            String patientTel = userLoginForm.getUserTel();
            String logoLink = userLoginForm.getLogoLink();
            SimpleDateFormat f1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String messageTime = f1.format(chatRecord.getChatTime());
            friendsList.setContent(content);
            friendsList.setLogoLink(logoLink);
            friendsList.setName(patientName);
            friendsList.setTime(messageTime);
            System.out.println(isPicture+" "+patientTel);
            chatRecords = chatRecordMapper.selectPatientCustomerServiceUnRead(patientIds.get(i));
            unReadNum = chatRecords.size();
            System.out.println(unReadNum+" "+patientName);
            if (unReadNum > 0) {
                friendsList.setIsRead(1);
            }
//            try{
//                friendsList.setIsPicture(isPicture);
//                friendsList.setPatientTel(patientTel);
//            }
//            catch (Exception e){
//                e.printStackTrace();
//            }
            if(isPicture==null) {
                isPicture=0;
            }
            friendsList.setIsPicture(isPicture);
            friendsList.setPatientTel(patientTel);

            System.out.println(isPicture+" "+patientTel);
            friendsLists.add(friendsList);
        }
        System.out.println("助理列表实际输出的数量:"+friendsLists.size());
        return friendsLists;
    }

    @Override
    public ArrayList<ChatContentView> showChatContent1(Integer assistantId, Integer doctorId){
        ArrayList<ChatContentView> chatContentViews=new ArrayList<>();
        ArrayList<ChatRecord> chatRecords=chatRecordMapper.selectByTwoId1(assistantId,doctorId);
        for(int i=0;i<chatRecords.size();i++)
        {
            ChatRecord chatRecord=chatRecords.get(i);
            if(chatRecord.getReceiverId().equals(assistantId)){
                chatRecordMapper.updateByChatId(chatRecord.getChatId());
            }
            ChatContentView chatContentView=new ChatContentView();
            UserLoginForm userLoginForm=userLoginFormMapper.selectByPrimaryKey(chatRecord.getSenderId());
            chatContentView.setOtherName(userLoginForm.getUserName());
            chatContentView.setOtherImg(userLoginForm.getLogoLink());
            SimpleDateFormat f1=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String messageTime = f1.format(chatRecord.getChatTime());
            String isRight,isImg;
            if(chatRecord.getSenderId()==assistantId)
            {
                isRight="true";
            }
            else {
                isRight="false";
            }
            if(chatRecord.getFlag2()==1){
                isImg="true";
            }
            else{
                isImg="false";
            }
            chatContentView.setTime(messageTime);
            chatContentView.setIsRight(isRight);
            chatContentView.setContent(chatRecord.getChatContent());
            chatContentView.setIsImg(isImg);
            chatContentView.setPhotoLink(chatRecord.getPhotoLink());
            chatContentViews.add(chatContentView);
        }
        return chatContentViews;
    }

    @Override
    public ArrayList<ChatContentView> showChatContent(Integer assistantId, Integer patientId){
        ArrayList<ChatContentView> chatContentViews=new ArrayList<>();
        ArrayList<ChatRecord> chatRecords=chatRecordMapper.selectByTwoId1(patientId,0);
        for(int i=0;i<chatRecords.size();i++)
        {
            ChatRecord chatRecord=chatRecords.get(i);
            if(chatRecord.getReceiverId().equals(0)){
                chatRecordMapper.updateByChatId(chatRecord.getChatId());
            }
            ChatContentView chatContentView=new ChatContentView();
            if(chatRecord.getSenderId()==0){
                UserLoginForm userLoginForm=userLoginFormMapper.selectByPrimaryKey(chatRecord.getAssistantId());
                chatContentView.setOtherName(userLoginForm.getUserName());
                chatContentView.setOtherImg(userLoginForm.getLogoLink());
            }
            else{
                UserLoginForm userLoginForm=userLoginFormMapper.selectByPrimaryKey(chatRecord.getSenderId());
                chatContentView.setOtherName(userLoginForm.getUserName());
                chatContentView.setOtherImg(userLoginForm.getLogoLink());
            }
            SimpleDateFormat f1=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String messageTime = f1.format(chatRecord.getChatTime());
            String isRight,isImg;
            if(chatRecord.getAssistantId()==null){
                chatRecord.setAssistantId(0);
            }
            if(chatRecord.getSenderId()==0&&chatRecord.getAssistantId()==assistantId){
                isRight="true";
            }
            else{
                isRight="false";
            }
            System.out.println(isRight);
            if(chatRecord.getFlag2()==1){
                isImg="true";
            }
            else{
                isImg="false";
            }
            chatContentView.setTime(messageTime);
            chatContentView.setIsRight(isRight);
            chatContentView.setContent(chatRecord.getChatContent());
            chatContentView.setIsImg(isImg);
            chatContentView.setPhotoLink(chatRecord.getPhotoLink());
            chatContentViews.add(chatContentView);

        }
        return chatContentViews;
    }

    @Override
    public ArrayList<ChatContentView> showChatContent2(Integer assistantId, Integer doctorId, Integer patientId){
        ArrayList<ChatContentView> chatContentViews=new ArrayList<>();
        ArrayList<ChatRecord> chatRecords=chatRecordMapper.selectByTwoId1(doctorId,patientId);
        for(int i=0;i<chatRecords.size();i++)
        {
            ChatRecord chatRecord=chatRecords.get(i);
            if(chatRecord.getReceiverId().equals(doctorId)){
                chatRecordMapper.updateByChatId(chatRecord.getChatId());
            }
            ChatContentView chatContentView=new ChatContentView();
            if (chatRecord.getFlag1()==null) {
                chatRecord.setFlag1(0);
            }
            if(chatRecord.getSenderId()==doctorId&&chatRecord.getFlag1()==0){
                UserLoginForm userLoginForm=userLoginFormMapper.selectByPrimaryKey(chatRecord.getAssistantId());
                if(userLoginForm==null) {
                    chatContentView.setOtherName("虚拟助理");
                    chatContentView.setOtherImg("");
                }
                else {
                    chatContentView.setOtherName(userLoginForm.getUserName());
                    chatContentView.setOtherImg(userLoginForm.getLogoLink());
                }

            }
            else{
                UserLoginForm userLoginForm=userLoginFormMapper.selectByPrimaryKey(chatRecord.getSenderId());
                chatContentView.setOtherName(userLoginForm.getUserName());
                chatContentView.setOtherImg(userLoginForm.getLogoLink());
            }
            SimpleDateFormat f1=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String messageTime = f1.format(chatRecord.getChatTime());
            String isRight,isImg;
            if(chatRecord.getAssistantId()==null){
                chatRecord.setAssistantId(0);
            }
            if(chatRecord.getFlag1()==null){
                chatRecord.setFlag1(1);
            }
            if(chatRecord.getAssistantId()==assistantId&&chatRecord.getFlag1()==0){
                isRight="true";
            }
            else{
                isRight="false";
            }
            if(chatRecord.getFlag2()==1){
                isImg="true";
            }
            else{
                isImg="false";
            }
            chatContentView.setTime(messageTime);
            chatContentView.setIsRight(isRight);
            chatContentView.setContent(chatRecord.getChatContent());
            chatContentView.setIsImg(isImg);
            chatContentView.setPhotoLink(chatRecord.getPhotoLink());
            chatContentViews.add(chatContentView);

        }
        return chatContentViews;
    }


    @Override
    public int insertChatContent(ChatRecord chatRecord){
        int a=0;
        try{
            a=chatRecordMapper.insertSelective(chatRecord);
        }
        catch (Exception e){
            System.out.println("插入失败");
        }
        return  a;
    }

    @Override
    public int deleteContactPerson(Integer senderId,Integer receiverId){
        int a=0;
        try{
            ArrayList<ChatRecord> chatRecords=chatRecordMapper.selectByTwoId1(senderId,receiverId);
            for(int i=0;i<chatRecords.size();i++){
                a=chatRecordMapper.updateAssistantFlagByChatId(chatRecords.get(i).getChatId());
            }
        }
        catch (Exception e){
            System.out.println("删除联系人错误");
        }
        return  a;
    }





}
